﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class YGAAUF : UIGAAUF
    {
        #region ==dwReq===========
        public static readonly DependencyProperty dwReqProperty = DependencyProperty.Register("dwReq",
        typeof(DataRow), typeof(YGAAUF),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwReqChanged));

        public DataRow dwReq
        {
            get
            {
                return (DataRow)GetValue(dwReqProperty);
            }
            set
            {
                SetValue(dwReqProperty, value);
                LOADBYREQ();
            }
        }
        private static void OdwReqChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as YGAAUF;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwReq = row;
            }
        }
        #endregion

        #region ==dwSM===========
        public static readonly DependencyProperty dwSMProperty = DependencyProperty.Register("dwSM",
        typeof(DataRow), typeof(YGAAUF),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwSMChanged));

        public DataRow dwSM
        {
            get
            {
                return (DataRow)GetValue(dwSMProperty);
            }
            set
            {
                SetValue(dwSMProperty, value);
            }
        }
        private static void OdwSMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as YGAAUF;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwSM = row;
            }
        }
        #endregion

        #region ==dwYM===========
        public static readonly DependencyProperty dwYMProperty = DependencyProperty.Register("dwYM",
        typeof(DataRow), typeof(YGAAUF),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwYMChanged));

        public DataRow dwYM
        {
            get
            {
                return (DataRow)GetValue(dwYMProperty);
            }
            set
            {
                SetValue(dwYMProperty, value);
            }
        }
        private static void OdwYMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as YGAAUF;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwYM = row;
            }
        }
        #endregion

        #region ==EXTMU===========
        public static readonly DependencyProperty EXTMUProperty = DependencyProperty.Register("EXTMU",
            typeof(string), typeof(YGAAUF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXTMUChanged));
        public string EXTMU
        {
            get
            {

                return (string)GetValue(EXTMUProperty);
            }
            set
            {
                SetValue(EXTMUProperty, value);
                if (MV == null)
                    return;
            }
        }
        private static void OnEXTMUChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as YGAAUF;
            if (src != null)
            {
                src.EXTMU = (string)e.NewValue;
            }
        }
        #endregion

        #region ==CHKRM===========
        public static readonly DependencyProperty CHKRMProperty = DependencyProperty.Register("CHKRM",
            typeof(bool), typeof(YGAAUF),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCHKRMChanged));
        public bool CHKRM
        {
            get
            {

                return (bool)GetValue(CHKRMProperty);
            }
            set
            {
                SetValue(CHKRMProperty, value);
                if (MV == null)
                    return;
            }
        }
        private static void OnCHKRMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as YGAAUF;
            if (src != null)
            {
                src.CHKRM = (bool)e.NewValue;
            }
        }
        #endregion

        public YGAAUF()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            InitializeComponent();
        }
        public void LOADBYREQ()
        {
            TBMain.Clear();
            if (dwReq == null)
                return;
            dwData = TBMain.NewRow();
            dwData["GAMANDANT"] = dwReq["GAMANDANT"];
            dwData["GAART"] = dwReq["GAART"];
            dwData["GAVAR"] = dwReq["GAVAR"];
            dwData["GAFARBE"] = dwReq["GAFARBE"];
            dwData["GAAUFMSL"] = dwReq["GAAUFMSL"];
            dwData["GAAENDBEN"] = POSTYP;
            SResult = ((VGAAUF)MV).QueryGAAUF(dwData, 23);
            MergeData(SResult, true, true);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);

            try
            {
                if (dwGD == null)
                {
                    dwSM = null;
                    dwYM = null;
                    return;
                }
                else
                {
                    if (EXTMU.Trim().Length > 0)
                    {
                        if (EXTMU != dwGD["GAAUFMSL"].ToString())
                        {
                            DataRow dwTemp = TBMain.Clone().Rows.Add();
                            dwTemp.ItemArray = dwGD.ItemArray;
                            dwTemp["GAAUFMSL"] = EXTMU;
                            dwSM = dwTemp;
                        } 
                    }
                    if (CHKRM)
                    {
                        DataRow dwGS = VGASTGA.Instance.TBMain.NewRow();
                        dwGS["GAGMANDANT"] = app.Mandant;
                        dwGS["GAGART"] = dwGD["GAART"];
                        dwGS["GAGVAR"] = dwGD["GAVAR"];
                        VGASTGA.Instance.QueryGASTGA(dwGS, 9);
                        DataTable TB = VGASTGA.Instance.TBMain.Copy();
                        if (TB.Rows.Count > 0)
                        {
                            dwYM = TB.Rows[0];
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
